<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 16:01
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\common\business;


use app\common\lib\Show;
use app\common\validate\Auth as ValObj;
use app\common\model\mysql\Auth as ModelObj;
use think\Exception;
use think\facade\Log;

class Auth extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }
    public function add(array $data = []): bool
    {
        $param['nickName'] = trim($data['name']);
        $param['sex'] = $data['sex'];
        $param['mobile'] = $data['mobile'];
        $param['openid'] = $data['openid'];
        $param['wx'] = $data['wx'];
        $param['school'] = $data['school'];
        $param['xueji'] = $data['student'];
        $param['idCard'] = $data['idCard'];
        $param['idCard'] = $data['idCard'];
        $param['state'] = 0;
        $param['idPath'] = $data['idCardImg'][0]['url'] ?? '';
        $param['idPathF'] = $data['idCardImg'][1]['url'] ?? '';
        $param['idSchool'] = $data['studentIdCard'];
        $param['schoolPath'] = $data['idCardImgFile'][0]['url'] ?? '';
        $this->isValidateAdd($param);
        try {
            $res = $this->modelObj->getAuthByOpenid($param['openid']);
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        if($res){
            $result = $res->save($param);
        }else{
            $result = $this->modelObj->insertAdd($param);
        }
        if($result){
            return true;
        }else{
            return false;
        }
    }

    /**
     * @param string $openid
     * @return ModelObj|array|\think\Model|null
     * @throws Exception
     */
    public function getAuthByOpenid(string $openid = '')
    {
        try {
            $user = $this->modelObj->getAuthByOpenid($openid);
        } catch (\Exception $e) {
            throw new Exception("查询失败");
        }
        return $user;
    }


}